package com.aaa.web.service.impl.schedulescanbatteryserviceimpl;

import com.aaa.entity.BatteryCharge;
import com.aaa.web.dao.schedulescanbatterydao.ScheduleScanBatteryDao;
import com.aaa.web.service.schedulescansbatteryservice.ScheduleScanBatteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * com.aaa.web.service.impl.schedulescanbatteryserviceimpl class
 *
 * @author Kevin Jia
 * @CLASS:ScheduleScanBatteryServiceImpl
 * @Date:2018/12/27 16:19
 */
@Component
public class ScheduleScanBatteryServiceImpl implements ScheduleScanBatteryService {

    /***每1分钟执行下任务*/
    public final static long ONE_MINUTE =  1 * 60 * 1000;


    @Autowired
    private ScheduleScanBatteryDao scheduleScanBatteryDao;

    /**
     * @Description: 定时任务,定时检查电瓶充电情况
     * @Param: none
     * @return: void
     * @Author: Kevin Jia
     * @Date: 2018/12/27
     * @Time: 16:29
     */
    @Scheduled(fixedDelay=ONE_MINUTE)
    public void scanBatterys(){
        //假设5个小时充满一个电瓶,获取系统时间-5h
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        long time = 1000*60*60*5;
        date = new Date(date.getTime()-time);
        //查询充电表充满电瓶数量
        Integer chargingCount = scheduleScanBatteryDao.getBatteryCounts(date);
        //如果有充满电,可有数量+chargingCount
        if (chargingCount>0){
            //查询充电表充满电瓶
            List<BatteryCharge> batteryCharges = scheduleScanBatteryDao.checkBatterysStatus(date);
            //修改充电表状态
            scheduleScanBatteryDao.updataBatteryStatus(batteryCharges);
            //修改站点表可用电瓶数量
            for (BatteryCharge bc: batteryCharges) {
                scheduleScanBatteryDao.updataBatteryPlace(bc);
            }
            //修改可用电瓶数量正常
            scheduleScanBatteryDao.isNormal();

        }

    }
}
